<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inventory extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
    	if (!isset($_SESSION['username'])){
	    	redirect('administration','refresh');
	    }
        elseif($_SESSION['username']=='dara' && $_SESSION['branch']=='00HO00')
        {
            redirect('itstock','refresh');
        }
        $this->load->model('MCategory');
        $this->load->model('MStock');
        $this->load->model('MStatic');
	    $this->load->helper('csv');
    }
/*---stationery----*/    
    public function stationeryList()
    {
        if(isset($_SESSION['group']) && $_SESSION['group'] == 'JU'){
            redirect('inventory/stationeryReport','refresh');
        }
        $data['title'] = "Manage Stationery";
		$data['main'] = 'stationerylist';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stationery';
        $data['stationery'] = $this->MStock->getStockList(1);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchStationery()
    {
        if($this->input->post('search') || $this->input->post('datepurchased'))
        {
            $data['title'] = "Manage Stationery";
    		$data['main'] = 'stationerylist';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stationery';
            $data['stationery'] = $this->MStock->getStockList(1, $this->input->post('search'), $this->input->post('datepurchased'));
    		$this->load->vars($data);
    		$this->load->view('layout');    
        }else{
            redirect('inventory/stationerylist','refresh');
        }
    }
    
    public function stationeryAdd()
    {
       if($this->input->post('item'))
       {
            $this->MStock->addEditStock();
	  		$this->session->set_flashdata('message','Stock added');
	  		redirect('inventory/stationerylist','refresh');
       }
		$data['title'] = "Add New Item";
		$data['main'] = 'stationeryadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('index.php/inventory/stationerylist');
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function stationeryEdit($id=0)
    {
       if($this->input->post('item'))
       {
            $this->MStock->addEditStock(id_clean($this->input->post('stock_id')));
	  		$this->session->set_flashdata('message','Stock updated');
	  		redirect('inventory/stationerylist','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'stationeryedit';
        $dstock = $this->MStock->getStockById($id);
        $data['stock'] = $dstock;
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/stationerylist','index.php/inventory/deleteStationery/'.$id);
        
        if($dstock['assign_id'] != null && $dstock['assign_id'] != ''){
            $data['form'] = $this->MFormGlobal->getButtonsEditDisableDel('index.php/inventory/stationerylist','?#');
        }
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
		$this->load->vars($data);
		$this->load->view('layout'); 
    }  
    
    public function stationeryUsed()
    {
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
		$data['main'] = 'stationeryused';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stationery';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['items'] = $this->MStock->getStationeryUsed()->result();
		$this->load->vars($data);
		$this->load->view('layout'); 
    } 
    
    public function stationeryBroken()
    {
        $data['title'] = "List Broken Items in Stock Inventory System";
		$data['main'] = 'stationerybroken';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stationery';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['items'] = $this->MStock->getStationeryBroken()->result();
		$this->load->vars($data);
		$this->load->view('layout'); 
    } 
    
    public function searchStationeryBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        if($start || $end || $branch)
        {
            $data['title'] = "List Broken Items in Stock Inventory System";
    		$data['main'] = 'stationeryused';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stationery';
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['items'] = $this->MStock->getStationeryBroken($start,$end, $branch)->result();
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('inventory/brokenlist','refresh');
        }
    }
    
    public function exportStationeryBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $data['title'] = "List Broken Items in Stock Inventory System";
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['items'] = $this->MStock->getStationeryBroken($start, $end, $branch)->result();
        //query_to_csv($items,TRUE,'Stationery_Broken_'.date('dMy').'.csv');
        $data['xlname'] = "StationeryBroken";
        $this->load->vars($data);
  		$this->load->view('stationerybroken_exp');
    }
    
    
    public function stationeryUsedEdit($id=0, $eleId=0)
    {
       if($this->input->post('unitused'))
       {
            $this->MStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
	  		$this->session->set_flashdata('message','Item updated');
	  		redirect('inventory/stationeryused','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'stationeryusededit';
        $data['stock'] = $this->MStock->getItemByStockIdItemId($id, $eleId);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/stationeryused','index.php/inventory/deleteStationeryUsed/'.$eleId);
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchStationeryUsed()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        if($start || $end || $branch)
        {
            $data['title'] = "List of Used and Transfered Stationery Items in Stock Inventory System";
    		$data['main'] = 'stationeryused';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stationery';
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['items'] = $this->MStock->getStationeryUsed($start, $end, $branch)->result();
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('inventory/stationeryused','refresh');
        }
    }
    
    public function exportStationeryUsed()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        //$items = $this->MStock->getStationeryUsed($start, $end, $branch, true);
        //query_to_csv($items,TRUE,'Stationery_Used_'.date('dMy').'.csv');
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['title'] = "List of Used and Transfered Stationery Items";
        $data['items'] = $this->MStock->getStationeryUsed($start, $end, $branch, true)->result();
        $data['xlname'] = "StationeryUsed";
        $this->load->vars($data);
  		$this->load->view('stationeryused_exp');
    }
    
    public function stationeryReport()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $submit = $this->input->post('submit');
        
        if($submit)
        {
            $data['title'] = "Stationery Report";
            $data['main'] = 'stationeryreport';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stationery';
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['cat'] = 1;
            $data['totalcount'] = $this->MStock->getTotal(1);
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $reports = $this->MStock->getReportPrinting(1, $start, $end, $branch);
            if($submit == "export" && (is_array($reports->result()) && count($reports->result()))){
                //var_dump($reports->result());
                //report_printing_csv($reports->result(),TRUE,'Stationery_Report_'.date('dMy').'.csv');
                if(!empty($branch))
                {
                    $data['xlbranch'] = $branch;   
                }else{
                    $data['xlbranch'] = $_SESSION['branch'];
                }
                $data['xlname'] = "StationeryReport";
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('report_xls');     
            }
            else
            {
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('layout'); 
            }   
        }
        else{
            $data['title'] = "Stationery Report";
            $data['main'] = 'stationeryreport';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stationery';
            $data['branchList'] = $this->MStatic->getBranchDropdown();
    		$this->load->vars($data);
    		$this->load->view('layout');
        } 
    }
    
    public function deleteStationery($id=0)
    {
        $this->MStock->deleteStock($id);
  		$this->session->set_flashdata('message','item deleted');
  		redirect('inventory/stationerylist','refresh');   
    }
    
    public function deleteStationeryUsed($id=0)
    {
        $this->MStock->deleteItem($id);
  		$this->session->set_flashdata('message','Item deleted');
  		redirect('inventory/stationerylist','refresh');   
    }
	    
/*---end stationery----*/

	public function index()
	{
		$data['title'] = "Dashboard - Stock Inventory System";
		$data['main'] = 'dashboard';
		$this->load->vars($data);
		$this->load->view('layout'); 
	}
    
    public function stockList()
    {
        if(isset($_SESSION['group']) && $_SESSION['group'] == 'JU'){
            redirect('inventory/printReport','refresh');
        }
        $data['title'] = "Manage Stock Inventory System";
		$data['main'] = 'stocklist';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stock';
        $data['stocks'] = $this->MStock->getStockList(2);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchStock()
    {
        if($this->input->post('search') || $this->input->post('datepurchased'))
        {
            $data['title'] = "Manage Stock Inventory System";
    		$data['main'] = 'stocklist';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stock';
            $data['stocks'] = $this->MStock->getStockList(2, $this->input->post('search'), $this->input->post('datepurchased'));
    		$this->load->vars($data);
    		$this->load->view('layout');    
        }else{
            redirect('inventory/stocklist','refresh');
        }
    }
    
    public function itemList()
    {
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
		$data['main'] = 'itemlist';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['printList'] = $this->MStatic->getTypeDropdownSearch();
        $data['items'] = $this->MStock->getItemsUsed()->result();
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function brokenList()
    {
        $data['title'] = "List Broken Items in Stock Inventory System";
		$data['main'] = 'brokenlist';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['items'] = $this->MStock->getItemsBroken()->result();
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        if($start || $end || $branch)
        {
            $data['title'] = "List Broken Items in Stock Inventory System";
    		$data['main'] = 'brokenlist';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stock';
             $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['items'] = $this->MStock->getItemsBroken($start,$end, $branch)->result();
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('inventory/brokenlist','refresh');
        }
    }
    
    public function searchItem()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $type = $this->input->post('type');
        if($start || $end || $branch || $type)
        {
            $data['title'] = "List Used and Transfered Items in Stock Inventory System";
    		$data['main'] = 'itemlist';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stock';
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['type'] = $type; 
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['printList'] = $this->MStatic->getTypeDropdownSearch();
            $data['items'] = $this->MStock->getItemsUsed($start, $end, $branch, false, $type)->result();
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('inventory/itemlist','refresh');
        }
    }
    
    public function exportItem()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $type = $this->input->post('type');
        //$items = $this->MStock->getItemsUsed($start, $end, $branch, true, $type);
        //query_to_csv($items,TRUE,'Printing_Used_'.date('dMy').'.csv');
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
        $data['items'] = $this->MStock->getItemsUsed($start, $end, $branch, true, $type)->result();
        $data['xlname'] = "PrintingUsed";
        $this->load->vars($data);
  		$this->load->view('itemlist_exp');
    }
    
    public function exportBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        //$items = $this->MStock->getItemsBroken($start, $end, $branch, true);
        //query_to_csv($items,TRUE,'Printing_Broken_'.date('dMy').'.csv');
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['title'] = "List Broken Items in Stock Inventory System";
        $data['items'] = $this->MStock->getItemsBroken($start, $end, $branch, true)->result();
        $data['xlname'] = "PrintingBroken";
        $this->load->vars($data);
  		$this->load->view('brokenlist_exp');
    }
    
    public function itemEdit($id=0, $eleId=0)
    {
       if($this->input->post('unitused'))
       {
            $this->MStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
	  		$this->session->set_flashdata('message','Item updated');
	  		redirect('inventory/itemlist','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'itemedit';
        $data['stock'] = $this->MStock->getItemByStockIdItemId($id, $eleId);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/itemlist','index.php/inventory/deleteitem/'.$eleId);
        $data['categoryList'] = $this->MCategory->getCategoryDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['printList'] = $this->MStatic->getTypeDropdown();
        $data['contractList'] = $this->MStatic->getContractDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function printReport()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $type = $this->input->post('type');
        $submit = $this->input->post('submit');
        
        if($submit)
        {
            $data['title'] = "Printing Report";
            $data['main'] = 'printreport';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stock';
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['type'] = $type;
            $data['cat'] = 2;
            $data['totalcount'] = $this->MStock->getTotal(2,$type);
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['printList'] = $this->MStatic->getTypeDropdownSearch();
            $reports = $this->MStock->getReportPrinting(2, $start, $end, $branch, $type);
            
            if($submit == "export" && (is_array($reports->result()) && count($reports->result()))){
                //report_printing_csv($reports->result(),TRUE,'Printing_Report_'.date('dMy').'.csv');
                if(!empty($branch))
                {
                    $data['xlbranch'] = $branch;   
                }else{
                    $data['xlbranch'] = $_SESSION['branch'];
                }
                $data['xlname'] = "PrintingReport";
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('report_xls');     
            }
            else
            {
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('layout'); 
            }   
        }
        else{
            $data['title'] = "Printing Report";
            $data['main'] = 'printreport';
            $data['leftside'] = 'content-left';
            $data['lefttype'] = 'stock';
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['printList'] = $this->MStatic->getTypeDropdownSearch();
    		$this->load->vars($data);
    		$this->load->view('layout');
        } 
    }
    
    public function stockAdd()
    {
       if($this->input->post('item'))
       {
            $this->MStock->addEditStock();
	  		$this->session->set_flashdata('message','Stock added');
	  		redirect('inventory/stocklist','refresh');
       }
		$data['title'] = "Add New Stock";
		$data['main'] = 'stockadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('index.php/inventory/stocklist');
        $data['categoryList'] = $this->MCategory->getCategoryDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['printList'] = $this->MStatic->getTypeDropdown();
        $data['contractList'] = $this->MStatic->getContractDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $rowor = $this->MStock->getMaxRefNo(1);
        $data['maxRefOR'] = $this->MStock->generateRefNum($rowor);
        $rowdv = $this->MStock->getMaxRefNo(2);
        $data['maxRefDV'] = $this->MStock->generateRefNum($rowdv);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function stockEdit($id=0)
    {
       if($this->input->post('item'))
       {
            $this->MStock->addEditStock(id_clean($this->input->post('stock_id')));
	  		$this->session->set_flashdata('message','Stock updated');
	  		redirect('inventory/stocklist','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'stockedit';
        $dstock = $this->MStock->getStockById($id);
        $data['stock'] = $dstock;
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/stocklist','index.php/inventory/deletestock/'.$id);
        
        if($dstock['assign_id'] != null && $dstock['assign_id'] != ''){
            $data['form'] = $this->MFormGlobal->getButtonsEditDisableDel('index.php/inventory/stocklist','?#');
        }
        $data['categoryList'] = $this->MCategory->getCategoryDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['printList'] = $this->MStatic->getTypeDropdown();
        $data['contractList'] = $this->MStatic->getContractDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $rowor = $this->MStock->getMaxRefNo(1);
        $data['maxRefOR'] = $this->MStock->generateRefNum($rowor);
        $rowdv = $this->MStock->getMaxRefNo(2);
        $data['maxRefDV'] = $this->MStock->generateRefNum($rowdv);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }   
    
    function refEdit($type=0, $stock=0)
    {
       if($this->input->post('item'))
       {
            $this->MStock->editDefaultItem(id_clean($this->input->post('stock_id')));
	  		$this->session->set_flashdata('message','Item updated');
            if(id_clean($this->input->post('type_id'))==1)
                redirect('inventory/settingor','refresh');
            else
	  		    redirect('inventory/settingdv','refresh');
       }
		$data['title'] = "Update Default Ref Number";
		$data['main'] = 'refedit';
        $data['stock'] = $this->MStock->getStockById($stock);
        if($type ==1)
            $data['form'] = $this->MFormGlobal->getFormEditCancelBottom('index.php/inventory/settingor');
        else 
            $data['form'] = $this->MFormGlobal->getFormEditCancelBottom('index.php/inventory/settingdv');
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    function settingor()
    {
        $data['title'] = "Default Ref Number for Printing Type OR";
		$data['main'] = 'orref';
        $data['items'] = $this->MStock->getDefaultItemByType(1);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    function settingdv()
    {
        $data['title'] = "Default Ref Number for Printing Type DV";
		$data['main'] = 'dvref';
        $data['items'] = $this->MStock->getDefaultItemByType(2);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function deleteStock($id=0)
    {
        $this->MStock->deleteStock($id);
        //$this->MStock->deleteStockFromItem($id);
  		$this->session->set_flashdata('message','item deleted');
  		redirect('inventory/stocklist','refresh');   
    }
    
    public function deleteItem($id=0)
    {
        //$this->MStock->setStatusInStock($id);
        $this->MStock->deleteItem($id);
        $this->MStock->deleteStockByAssignId($id);
  		$this->session->set_flashdata('message','Item deleted');
  		redirect('inventory/itemlist','refresh');   
    }

    
/*---FA & NA----*/    
    public function assetList($id=0)
    {
        if($id != 3 && $id != 4)
        {
            redirect('/','refresh');
        }
        if(isset($_SESSION['group']) && $_SESSION['group'] == 'JU'){
            redirect('inventory/assetReport/'.$id,'refresh');
        }
        $data['title'] = "Manage Asset";
		$data['main'] = 'assetlist';
        $data['leftside'] = 'content-left';
        if($id == 3)
            $data['lefttype'] = 'assetFA';
        else
            $data['lefttype'] = 'assetNA';
        $data['assets'] = $this->MStock->getStockList($id);
        $data['cat'] = $id;
		$this->load->vars($data); 
		$this->load->view('layout'); 
    }
    
    public function searchAsset()
    {
        $cat = $this->input->post('cat');
        if($this->input->post('search') || $this->input->post('datepurchased'))
        {
            $data['title'] = "Manage Asset";
    		$data['main'] = 'assetlist';
            $data['leftside'] = 'content-left';
            if($cat == 3)
                $data['lefttype'] = 'assetFA';
            else
                $data['lefttype'] = 'assetNA';
            
            $data['stationery'] = $this->MStock->getStockList($cat, $this->input->post('search'), $this->input->post('datepurchased'));
    		$this->load->vars($data);
    		$this->load->view('layout');    
        }else{
            redirect('inventory/assetlist/'.$cat,'refresh');
        }
    }
    
    public function assetAdd($cat=0)
    {
        if($this->input->post('item'))
        {
            $this->MStock->addEditStock(0, true);
	  		$this->session->set_flashdata('message','Stock added');
	  		$cat = $this->input->post('category_id');
            redirect('inventory/assetlist/'.$cat,'refresh');
        }
		$data['title'] = "Add New Item";
		$data['main'] = 'assetadd';
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $data['faList'] = $this->MStatic->getFADropdown();
        $data['naList'] = $this->MStatic->getNADropdown();
        $data['cat'] = $cat;
        $data['form'] = $this->MFormGlobal->getButtonsAdd('index.php/inventory/assetlist/'.$cat);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function assetEdit($id=0, $cat=0)
    {
        if($this->input->post('item'))
        {
            $this->MStock->addEditStock(id_clean($this->input->post('stock_id')));
	  		$this->session->set_flashdata('message','Stock updated');
            $cat = $this->input->post('category_id');
            redirect('inventory/assetlist/'.$cat,'refresh');
        }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'assetedit';
        $dstock = $this->MStock->getStockById($id);
        $data['stock'] = $dstock;
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/assetlist/'.$cat,'index.php/inventory/deleteAsset/'.$id.'/'.$cat);
        
        if($dstock['assign_id'] != null && $dstock['assign_id'] != ''){
            $data['form'] = $this->MFormGlobal->getButtonsEditDisableDel('index.php/inventory/assetlist/'.$cat,'?#');
        }
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['faList'] = $this->MStatic->getFADropdown();
        $data['naList'] = $this->MStatic->getNADropdown();
        $data['cat'] = $cat;
		$this->load->vars($data);
		$this->load->view('layout'); 
    }  
    
    public function assetUsed($cat=0)
    {
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');
        }
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
		$data['main'] = 'assetused';
        $data['leftside'] = 'content-left';
        $data['cat'] = $cat;
        if($cat == 3)
            $data['lefttype'] = 'assetFA';
        else
            $data['lefttype'] = 'assetNA';
            
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['items'] = $this->MStock->getAssetUsed($cat)->result();
		$this->load->vars($data);
		$this->load->view('layout'); 
    } 
    
    public function assetbroken($cat=0)
    {
        if($cat == 0 || $cat == '')
            $cat = $this->input->post('cat');
            
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');
        }
        $data['title'] = "List of Broken Items";
		$data['main'] = 'assetbroken';
        $data['leftside'] = 'content-left';
        $data['cat'] = $cat;
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        
        if($cat == 3)
            $data['lefttype'] = 'assetFA';
        else
            $data['lefttype'] = 'assetNA';
        
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        if($start || $end || $branch)
        {
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['items'] = $this->MStock->getAssetBroken($cat,$start, $end, $branch)->result();
        }
        else{
            $data['items'] = $this->MStock->getAssetBroken($cat)->result();
        } 
        $this->load->vars($data);
		$this->load->view('layout');
    }
    
    public function exportAssetBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $cat = $this->input->post('cat');
        //$items = $this->MStock->getAssetBroken($cat, $start, $end, $branch, true);
        //query_to_csv($items,TRUE,'Asset_Broken_'.date('dMy').'.csv');
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        if($cat == 3){
            $data['title'] = "FA Broken Items";
            $data['xlname'] = "FABroken";
        }else{
             $data['title'] = "NA Broken Items";
            $data['xlname'] = "NABroken";
        }
        $data['items'] = $this->MStock->getAssetBroken($cat, $start, $end, $branch, true)->result();
        $this->load->vars($data);
  		$this->load->view('assetbroken_exp');
    }
    
    public function assetUsedEdit($id=0, $eleId=0, $cat=0)
    {
       if($this->input->post('unitused'))
       {
            $this->MStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
	  		$this->session->set_flashdata('message','Item updated');
  		    $cat = $this->input->post('category_id');
            redirect('inventory/assetUsed/'.$cat,'refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'assetusededit';
        $data['stock'] = $this->MStock->getItemByStockIdItemId($id, $eleId);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('index.php/inventory/assetused/'.$cat,'index.php/inventory/deleteAssetUsed/'.$eleId.'/'.$cat);
        $data['statusList'] = $this->MStatic->getStatusDropdown(false);
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        $data['faList'] = $this->MStatic->getFADropdown();
        $data['naList'] = $this->MStatic->getNADropdown();
        $data['cat'] = $cat;
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchAssetUsed()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $cat = $this->input->post('cat');
        if($start || $end || $branch)
        {
            $data['title'] = "List Used and Transfered Items in Stock Inventory System";
    		$data['main'] = 'assetused';
            $data['leftside'] = 'content-left';
            if($cat == 3)
                $data['lefttype'] = 'assetFA';
            else
                $data['lefttype'] = 'assetNA';
            
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['cat'] = $cat;
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['items'] = $this->MStock->getAssetUsed($cat, $start, $end, $branch)->result();
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('inventory/assetused/'.$cat,'refresh');
        }
    }
    
    public function exportAssetUsed()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $cat = $this->input->post('cat');
        //$items = $this->MStock->getAssetUsed($cat, $start, $end, $branch, true);
        //query_to_csv($items,TRUE,'Asset_Used_'.date('dMy').'.csv');
        
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        if($cat == 3){
            $data['title'] = "FA Used Items";
            $data['xlname'] = "FAUsed";
        }else{
             $data['title'] = "NA Used Items";
            $data['xlname'] = "NAUsed";
        }
        $data['items'] = $this->MStock->getAssetUsed($cat, $start, $end, $branch, true)->result();
        $this->load->vars($data);
  		$this->load->view('assetused_exp');
    }
    
    public function assetReport($cat=0)
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $type = $this->input->post('type');
        $submit = $this->input->post('submit');
        
        if($submit)
        {
            $cat = $this->input->post('cat');
            $data['main'] = 'assetreport';
            $data['leftside'] = 'content-left';
            if($cat == 3){
                $data['lefttype'] = 'assetFA';
                $data['title'] = "FA Report";
                $xlname = "FAReport";
                $data['assetType'] = $this->MStatic->getFADropdownSearch();
            }
            else{
                $data['lefttype'] = 'assetNA';
                $data['title'] = "NA Report";
                $xlname = "NAReport";
                $data['assetType'] = $this->MStatic->getNADropdownSearch();
            }    
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['cat'] = $cat;
            $data['type'] = $type;
            $data['totalcount'] = $this->MStock->getTotal($cat,$type);
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $reports = $this->MStock->getReportPrinting($cat, $start, $end, $branch, $type);
            if($submit == "export" && (is_array($reports->result()) && count($reports->result()))){
                //report_printing_csv($reports->result(),TRUE,'Asset_Report_'.date('dMy').'.csv');   
                if(!empty($branch))
                {
                    $data['xlbranch'] = $branch;   
                }else{
                    $data['xlbranch'] = $_SESSION['branch'];
                }
                $data['xlname'] = $xlname;
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('report_xls');  
            }
            else
            {
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('layout'); 
            }
               
        }
        else{
            $data['main'] = 'assetreport';
            $data['leftside'] = 'content-left';
            
            if($cat == 3){
                $data['lefttype'] = 'assetFA';
                $data['title'] = "FA Report";
                $data['assetType'] = $this->MStatic->getFADropdownSearch();
            }
            else{
                $data['lefttype'] = 'assetNA';
                $data['title'] = "NA Report";
                $data['assetType'] = $this->MStatic->getNADropdownSearch();
            }    
                
            $data['cat'] = $cat;
            $data['branchList'] = $this->MStatic->getBranchDropdown();
    		$this->load->vars($data);
    		$this->load->view('layout');
        } 
    }
    
    public function deleteAsset($id=0, $cat=0)
    {
        $this->MStock->deleteStock($id);
  		$this->session->set_flashdata('message','item deleted');
  		redirect('inventory/assetlist/'.$cat,'refresh');   
    }
    
    public function deleteAssetUsed($id=0, $cat=0)
    {
        $this->MStock->deleteItem($id);
  		$this->session->set_flashdata('message','Item deleted');
  		redirect('inventory/assetused/'.$cat,'refresh');   
    }
    
    public function deleteBroken($id=0, $cat=0)
    {
        $this->MStock->deleteBroken($id);
  		$this->session->set_flashdata('message','item is moved!');
        if($cat == 1)
  		    redirect('inventory/stationerybroken','refresh'); 
        elseif($cat == 2)
  		    redirect('inventory/brokenlist','refresh');
        else
            redirect('inventory/assetbroken/'.$cat,'refresh');
    }
    
    public function totaladd($cat)
    {
        if($this->input->post('total'))
        {
            $this->MStock->addEditTotal();
	  		$this->session->set_flashdata('message','Total updated');
            if($cat==1){
                redirect('inventory/stationerylist','refresh');
            }
            elseif($cat==2){
                redirect('inventory/stocklist','refresh');
            }
            else{
                redirect('inventory/assetlist/'.$cat,'refresh');
            } 
        }
        $data['title'] = "Total Items";
		$data['main'] = 'total';
        $data['leftside'] = 'content-left';
        $data['cat'] = $cat;
        if($cat==1){
            $data['lefttype'] = 'stationery';
            $url = "stationerylist";
            $ls = null;
        }
        elseif($cat==2){
            $data['lefttype'] = 'stock';
            $url = "stocklist";
            $ls = $this->MStatic->getTypeDropdownSearch();
        }
        else{
            if($cat == 3)
            { 
                $data['lefttype'] = 'assetFA';
                $ls = $this->MStatic->getFADropdownSearch();
            }else{
                $data['lefttype'] = 'assetNA';
                $ls = $this->MStatic->getNADropdownSearch();
            }
            $url = "assetlist/".$cat;
        }
        //$data['dt'] = $this->MStock->getTotalByCatId($cat);
        $data['ls'] = $ls;
        $data['form'] = $this->MFormGlobal->getButtonsAdd('index.php/inventory/'.$url);
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function listTotal()
    {
        $cat = $this->input->post('cat_id');
        $type = $this->input->post('type_id');
        $row = $this->MStock->checkExistedTotal($cat, $type);
        if(is_array($row) && count($row)){
            echo $row['total'];
        }
    }
    
    public function listReturn()
    {
        $itemId = $this->input->post('item_id');
        $branch = $this->input->post('branch_id');
        $row = $this->MStock->checkExistedReturn($itemId, $branch);
        if(is_array($row) && count($row)){
            echo $row['unit'].','.$row['ref'];
        }
    }
    public function getReturn()
    {
        if($_SESSION['branch'] != '00HO00'){
            redirect('inventory/stocklist','refresh');
        }
        $data['title'] = "List of returned items";
		$data['main'] = 'returnlist';
        $data['leftside'] = 'content-left';
        $data['lefttype'] = 'stock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['printList'] = $this->MStatic->getTypeDropdownSearch();
        
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $branch = $this->input->post('branch');
        $type = $this->input->post('type');
        $submit = $this->input->post('submit');
        
        $data['datestart'] = $start;
        $data['dateend'] = $end;
        $data['branch'] = $branch; 
        $data['type'] = $type; 
        
        if($submit)
        {
             $reports = $this->MStock->getReturnList($start,$end,$branch,$type,$submit);
             if($submit == "export" && (is_array($reports) && count($reports))){   
                if(!empty($branch))
                {
                    $data['xlbranch'] = $branch;   
                }else{
                    $data['xlbranch'] = "All offices";
                }
                $data['xlname'] = 'ReturnedItems';
                $data['items'] = $reports;
        		$this->load->vars($data);
        		$this->load->view('returnlist_xls');  
            }else{
               $data['items'] = $reports;
   		       $this->load->vars($data);
    		   $this->load->view('layout');
            } 
            
        }else{
            $data['items'] = $this->MStock->getReturnList();
    		$this->load->vars($data);
    		$this->load->view('layout');
        }
    }
    
    public function acceptreturn($stockId,$branch)
    {
        $this->MStock->acceptReturn($stockId,$branch);
  		$this->session->set_flashdata('message','Item accepted');
        redirect('inventory/getReturn','refresh'); 
    }
	    
/*---end FA & NA----*/
    
    public function test()
    {
         //$this->MStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
         $this->MStock->addEditStock();
         echo 'test';
    }
}
